package com.gitee.wsl.result.tran

import com.gitee.wsl.result.sample.ValueResult
import kotlin.test.Test
import kotlin.test.assertEquals

class AndTest {
    private object AndError

    class And {

        @Test
        fun returnsValueIfOk() {
            assertEquals(
                expected = ValueResult.Ok(500),
                actual = ValueResult.Ok(230).and(ValueResult.Ok(500)),
            )
        }

        @Test
        fun returnsValueIfErr() {
            assertEquals(
                expected = ValueResult.Err("hello world"),
                actual = ValueResult.Ok(300).and(ValueResult.Err("hello world")),
            )
        }
    }

    class AndThen {

        @Test
        fun returnsTransformedValueIfOk() {
            assertEquals(
                expected = ValueResult.Ok(12),
                actual = ValueResult.Ok(5).andThen { ValueResult.Ok(it + 7) },
            )
        }

        @Test
        fun returnsErrorIfErr() {
            assertEquals(
                expected = ValueResult.Err(AndError),
                actual = ValueResult.Ok(20).andThen { ValueResult.Ok(it + 43) }
                    .andThen { ValueResult.Err(AndError) },
            )
        }
    }
}